<?php
declare (strict_types = 1);

namespace app\admin\middleware;

use app\common\service\AuthService;
use app\common\service\system\NodeService;

class Auth
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $current_node = parseNodeStr($request->controller().'/'.$request->action());
        if(!in_array($current_node,config('admin.no_need_auth'))){
            $permissions = app(NodeService::class)->getPermissionList($request->user_id);
            if(!in_array($current_node,$permissions)){
                return error("无权限访问",[$current_node,$permissions]);
            }else{
                return $next($request);
            }
        }
        return $next($request);
    }
}
